<template>
  <a-modal
    :open="open"
    width="640px"
    title="皮试结果"
    centered
    :ok-button-props="{ disabled: !isCompleted || formState.disabled }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" :colon="false" class="form-style-flex">
      <a-form-item label="开始时间" class="w-1/2" v-bind="validateInfos.startTime">
        <a-date-picker
          v-model:value="formState.startTime"
          show-time
          :disabled="formState.disabled"
          placeholder="请选择"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </a-form-item>
      <a-form-item label="结束时间" class="w-1/2" v-bind="validateInfos.endTime">
        <a-date-picker
          v-model:value="formState.endTime"
          show-time
          :disabled="formState.disabled"
          placeholder="请选择"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </a-form-item>
      <a-form-item label="皮试结果" class="w-full" v-bind="validateInfos.result">
        <a-radio-group v-model:value="formState.result" :disabled="formState.disabled">
          <a-radio :value="0">阴性(-)</a-radio>
          <a-radio :value="1">弱阳性(+)</a-radio>
          <a-radio :value="2">阳性(++)</a-radio>
          <a-radio :value="3">强阳性(+++)</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="结果观察" class="w-full">
        <a-textarea v-model:value="formState.resultDescribe" :disabled="formState.disabled" placeholder="请输入" :rows="5" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
import { ref, watch } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['update:open', 'success'])
const formState = ref({
  startTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  endTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
})
const rulesState = ref({
  startTime: [{ required: true, message: '请选择开始时间' }],
  endTime: [{ required: true, message: '请选择结束时间' }],
  result: [{ required: true, message: '请选择皮试结果' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  await validate()
  const { code, msg } = await post('/outp/mzhs/savePatientSkinTestResult', {
    ...formState.value,
    id: props.rowData?.id,
    appFrom: '2',
    status: '2'
  })
  if (code !== 0) return message.error(msg || '操作失败')
  message.success(msg)
  resetFields()
  emit('update:open', false)
  emit('success')
}
const onCancel = () => {
  resetFields()
  emit('update:open', false)
}
watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = {
        ...props.rowData,
        startTime: props.rowData.startTime || dayjs().format('YYYY-MM-DD HH:mm:ss'),
        // startTime: null,
        endTime: props.rowData.endTime || dayjs().format('YYYY-MM-DD HH:mm:ss')
        // endTime: null
      }
      // formState.value.startTime = dayjs(props.rowData.startTime).format('YYYY-MM-DD HH:mm:ss')
      // formState.value.endTime = dayjs(props.rowData.updateTime).format('YYYY-MM-DD HH:mm:ss')
      formState.value.disabled = props.rowData.status === '3' || props.rowData.status === '4' ? true : false
    }
  }
)
</script>
<style lang="less" scoped>
.ant-form {
  display: flex;
  flex-flow: row wrap;
  min-width: 0;
  :deep(.ant-form-item) {
    .ant-form-item-label {
      width: 90px;
    }
  }
  .ant-picker {
    width: 100%;
  }
}
</style>
